package tw.edu.tku.im.maze;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class UpdateGameRep extends RepPacket {

   String timestamp;
   MazeCell[] mark;
   MazeCell[] unmark;
   GameState gameState;
   
   //-------------------------------------------------------
   public UpdateGameRep(String timestamp, GameState gameState) {
      this(timestamp, null, null, gameState);
   }
   
   public UpdateGameRep(String timestamp, MazeCell[] mark, GameState gameState) {
      this(timestamp, mark, null, gameState);
   }
   
   public UpdateGameRep(String timestamp, MazeCell[] mark, MazeCell[] unmark, 
                        GameState gameState) 
   {
      this.timestamp = timestamp;
      this.mark = mark;
      this.unmark = unmark;
      this.gameState = gameState;
   }
   
   //-------------------------------------------------------
   public String getTimestamp() {
      return timestamp;
   }

   public void setTimestamp(String timestamp) {
      this.timestamp = timestamp;
   }

   public MazeCell[] getMark() {
      return mark;
   }

   public void setMark(MazeCell[] mark) {
      this.mark = mark;
   }

   public MazeCell[] getUnmark() {
      return unmark;
   }

   public void setUnmark(MazeCell[] unmark) {
      this.unmark = unmark;
   }

   public GameState getGameState() {
      return gameState;
   }

   public void setGameState(GameState gameState) {
      this.gameState = gameState;
   }
   
   //-------------------------------------------------------
   public String toJSONString() throws JSONException {
      JSONObject jsonObj = new JSONObject();
      JSONObject result = new JSONObject();
      result.put("timestamp", this.timestamp);
      if(this.mark!=null) {
         JSONArray mark = new JSONArray();
         for(MazeCell cell : this.mark) {
            if(cell==null) { break; }
            mark.put(new JSONObject(cell.toJSONString()));
         }
         result.put("mark", mark);
      }
      if(this.unmark!=null) {
         JSONArray unmark = new JSONArray();
         for(MazeCell cell : this.unmark) {
            if(cell==null) { break; }
            unmark.put(new JSONObject(cell.toJSONString()));
         }
         result.put("unmark", unmark);
      }
      result.put("gameState", new JSONObject(this.gameState.toJSONString()));
      jsonObj.put("result", result);
      jsonObj.put("action", "updateGameResponse");
      return jsonObj.toString();
   }

   @Override
   public String getPacketName() {
      return "updateGameResponse";
   }

   @Override
   public int describeContents() {
      return 0;
   }

   @Override
   public void writeToParcel(Parcel arg0, int arg1) {
      
   }

}
